home *** CD-ROM | disk | FTP | other *** search
- /* example XFun that processes index values. Computes a 3D cross product
-
- MathPad will call the function cross() 3 times to access 3 vector components.
- An "array" function must compute its return value based on the index.
- MathPad only evaluates the elements that it needs and there are no guarantees
- as to which elements will be evaluated or what order they are evaluated in.
-
- For computing the cross product, using GetExprMatrix() to get the input vectors
- is simple but is not the most efficient technique since it will evaluate all
- elements of both vectors on each call.
- */
-
- #include "callback.h"
-
- BOOL cross(extended *retval,funptr callback)
- {
- EXPR xpr;
- extended *a,*b,i;
- long rows,cols;
- BOOL ok;
-
- ok = TRUE;
- if(!GetParmVal(2,&i,callback)) return(FALSE); /* index value is parm 2 */
-
- MakeParmExpr(1,&xpr,callback); /* vector A is parm 1 */
- if(!GetExprMatrix(xpr,&a,&rows,&cols,callback)) ok = FALSE;
- FreeExpr(xpr,callback);
- if(!ok || rows !=3 || cols !=0)
- {
- DisposPtr(a);
- return(FALSE);
- }
- MakeParmExpr(0,&xpr,callback); /* vector B is parm 0 */
- if(!GetExprMatrix(xpr,&b,&rows,&cols,callback)) ok = FALSE;
- FreeExpr(xpr,callback);
- if(!ok || rows !=3 || cols !=0)
- {
- DisposPtr(a);
- DisposPtr(b);
- return(FALSE);
- }
-
- switch((int)i) /* calculate component for a given index value */
- {
- case 1: *retval = a[1] * b[2] - a[2] * b[1]; break;
- case 2: *retval = a[2] * b[0] - a[0] * b[2]; break;
- case 3: *retval = a[0] * b[1] - a[1] * b[0]; break;
- default: ok = FALSE;
- }
-
- DisposPtr(a);
- DisposPtr(b);
- return(ok);
- }
-
- void predef(funptr callback)
- {
- AddFunDim("cross",3,callback); /* cross(A,B)[i] = xfun(i,A,B) dim[3] */
- }
-
- void main(funptr callback)
- {
- AddXfun("cross","A,B",&cross,&predef,callback);
- }
-
-